//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.purvar.boot.comm.util;

import com.purvar.boot.api.entity.SysAutocodeDetail;
import com.purvar.boot.api.entity.SysAutocodeMain;
import com.purvar.boot.auto.config.AutoCodeConfig;
import com.purvar.boot.comm.exception.BusinessException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class AutoCodeUtils {
    private static AutoCodeConfig config = (AutoCodeConfig) SpringContextUtils.getBean(AutoCodeConfig.class);

    public AutoCodeUtils() {
    }

    private static String getProperty(String name) {
        String ret = "";
        if (StringUtils.isNotEmpty(config.getEnv().getProperty(name))) {
            ret = config.getEnv().getProperty(name);
        }

        return ret;
    }

    public static List<String> getTemplates() {
        List<String> templates = new ArrayList();
        templates.add("template/commonJava/Entity.java.vm");
        templates.add("template/commonJava/EntityVo.java.vm");
        templates.add("template/commonJava/mapper.java.vm");
        templates.add("template/commonJava/mapper.xml.vm");
        templates.add("template/onetable/java/controller.java.vm");
        templates.add("template/onetable/java/service.java.vm");
        templates.add("template/onetable/java/serviceImpl.java.vm");
        templates.add("template/onetable/vue/index.vue.vm");
        templates.add("template/onetable/js/api.js.vm");
        templates.add("template/onetable/js/local.js.vm");
        return templates;
    }

    public static byte[] genCode(SysAutocodeMain sysAutocodeMain, List<SysAutocodeDetail>
            sysAutocodeDetails) {
        Map<String, Object> map = new HashMap();
        String filedName = "";
        for (SysAutocodeDetail a : sysAutocodeDetails) {
            if ("1".equals(a.getMainkey())) {
                filedName = a.getJavaName();
            }
        }
        if (StringUtils.isEmpty(filedName)) {
            filedName = "id";
        }
        map.put("table", sysAutocodeMain);
        map.put("moudleName", sysAutocodeMain.getMoudleName());
        map.put("autoClassName", sysAutocodeMain.getAutoClassName());
        map.put("colums", sysAutocodeDetails);
        map.put("author", getProperty("author"));
        map.put("email", getProperty("email"));
        map.put("idField", filedName);
        map.put("IdField", GenUtils.toUpCaseFirstOne(filedName));


        map.put("datetime", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
        map.put("className", GenUtils.toLowerCaseFirstOne(sysAutocodeMain.getAutoClassName()));
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(prop);
        VelocityContext context = new VelocityContext(map);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        List<String> templates = getTemplates();
        Iterator var8 = templates.iterator();

        while (var8.hasNext()) {
            String template = (String) var8.next();
            StringWriter sw = new StringWriter();
            Template tpl = Velocity.getTemplate(template, "UTF-8");
            tpl.merge(context, sw);

            try {
                zip.putNextEntry(new ZipEntry(getFileName(template, sysAutocodeMain.getAutoClassName(), sysAutocodeMain.getAutoPackPath(), sysAutocodeMain.getMoudleName(), GenUtils.toLowerCaseFirstOne(sysAutocodeMain.getAutoClassName()))));
                IOUtils.write(sw.toString(), zip, "UTF-8");
                IOUtils.closeQuietly(sw);
                zip.closeEntry();
            } catch (IOException var13) {
                throw new BusinessException("渲染模板失败，表名：" + sysAutocodeMain.getAutoTableName(), new Object[]{var13});
            }
        }

        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }

    public static String getFileName(String template, String className, String packageName, String moduleName, String lowclassName) {
        String packagePath = "main" + File.separator + "java" + File.separator;
        if (StringUtils.isNotBlank(packageName)) {
            packagePath = packagePath + packageName.replace(".", File.separator) + File.separator;
        }

        if (template.contains("Entity.java.vm")) {
            packagePath = packagePath + "api" + File.separator + moduleName + File.separator;
            return packagePath + "entity" + File.separator + className + ".java";
        } else if (template.contains("EntityVo.java.vm")) {
            packagePath = packagePath + "api" + File.separator + moduleName + File.separator;
            return packagePath + "pojo" + File.separator + className + "Vo.java";
        } else if (template.contains("mapper.java.vm")) {
            packagePath = packagePath + "api" + File.separator + moduleName + File.separator;
            return packagePath + "mapper" + File.separator + className + "Mapper.java";
        } else if (template.contains("mapper.xml.vm")) {
            packagePath = packagePath + "api" + File.separator + moduleName + File.separator;
            return packagePath + "mapper" + File.separator + "xml" + File.separator + className + "Mapper.xml";
        } else if (template.contains("controller.java.vm")) {
            packagePath = packagePath + moduleName + File.separator;
            return packagePath + "controller" + File.separator + className + "Controller.java";
        } else if (template.contains("service.java.vm")) {
            packagePath = packagePath + "api" + File.separator + moduleName + File.separator;
            return packagePath + "service" + File.separator + "I" + className + "Service.java";
        } else if (template.contains("serviceImpl.java.vm")) {
            packagePath = packagePath + "api" + File.separator + moduleName + File.separator;
            return packagePath + "service" + File.separator + "impl" + File.separator + className + "ServiceImpl.java";
        } else if (template.contains("index.vue.vm")) {
            return "purchaser" + File.separator + moduleName + File.separator + lowclassName + File.separator + "index.vue";
        } else if (template.contains("api.js.vm")) {
            return "api" + File.separator + moduleName + File.separator + lowclassName + ".js";
        } else {
            return template.contains("local.js.vm") ? "purchaser" + File.separator + moduleName + File.separator + lowclassName + File.separator + "local.js" : null;
        }
    }
}
